package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@Api(tags = "分类相关接口")
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /* 新增分类 */
    @PostMapping
    @ApiOperation("新增分类")
    public Result addCategory(@RequestBody CategoryDTO dto) {
        categoryService.addCategory(dto);
        return Result.success();
    }

    /* 分页查询 */
    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result<PageResult> page(CategoryPageQueryDTO dto){
        log.info("分页查询:{}",dto);
        return Result.success(categoryService.page(dto));
    }

    /* 根据类型查询 */
    @GetMapping("/list")
    @ApiOperation("根据类型查询")
    public Result list(Integer type){
        log.info("根据类型查询:{}",type);
       return Result.success(categoryService.list(type));
    }

    /* 启用或禁用分类 */
    @ApiOperation("分类状态修改")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable("status") Integer status,Long id){
        categoryService.updateStatus(status,id);
        return Result.success();
    }

    /* 根据ID删除分类 */
    @ApiOperation("根据ID删除分类")
    @DeleteMapping
    public Result deleteCategory(Long id){
        categoryService.deleteCategory(id);
        return Result.success();
    }

    /* 修改分类 */
    @ApiOperation("修改分类")
    @PutMapping
    public Result updateCategory(@RequestBody CategoryDTO dto){
        categoryService.updateCategory(dto);
        return Result.success();
    }
}
